package com.zjw.touchscreen.listener;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.zjw.touchscreen.LoginActivity;
import com.zjw.touchscreen.MainActivity;
import com.zjw.touchscreen.config.RetrofitManager;
import com.zjw.touchscreen.db.DatabaseHelper;
import com.zjw.touchscreen.http.ApiInterface;
import com.zjw.touchscreen.http.entity.LoginRespVO;
import com.zjw.touchscreen.http.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginListener implements View.OnClickListener {

    private LoginActivity context;

    private EditText userName;

    private EditText password;

    private EditText captchaCode;

    private TextView captchaCodeUuid;

    private DatabaseHelper databaseHelper;

    public LoginListener(LoginActivity context, EditText userName, EditText password, EditText captchaCode, TextView captchaCodeUuid) {
        this.context = context;
        this.userName = userName;
        this.password = password;
        this.captchaCode = captchaCode;
        this.captchaCodeUuid = captchaCodeUuid;
    }

    @Override
    public void onClick(View v) {
        v.setEnabled(false);
        String userName = this.userName.getText().toString();
        String password = this.password.getText().toString();
        String captchaCode = this.captchaCode.getText().toString();
        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(captchaCode)) {
            v.setEnabled(true);
            Toast.makeText(context, "用户名、密码或验证码为空，请输入!", Toast.LENGTH_SHORT).show();
            return;
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(userName);
        userEntity.setPassword(password);
        userEntity.setCode(captchaCode);
        userEntity.setUuid(captchaCodeUuid.getText().toString());
        ApiInterface apiInterface = RetrofitManager.create(ApiInterface.class);
        Call<LoginRespVO> loginCall = apiInterface.login(userEntity);
        loginCall.enqueue(new Callback<LoginRespVO>() {
            @Override
            public void onResponse(Call<LoginRespVO> call, Response<LoginRespVO> response) {
                v.setEnabled(true);
                LoginRespVO responseData = response.body();
                int code = responseData.getCode();
                if (code != 200) {
                    context.refreshCaptchaCodeImage();
                    Toast.makeText(context, "登陆失败!" + responseData.getMsg(), Toast.LENGTH_SHORT).show();
                    return;
                }
                String token = responseData.getToken();
                Log.e("onResponse: token ", token);
                databaseHelper = DatabaseHelper.getInstance(context, "touchscreen", 1);
                SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
                writableDatabase.delete("user", null, null);
                ContentValues values = new ContentValues();
                values.put("name", userName);
                values.put("pwd", password);
                values.put("token", token);
                writableDatabase.insert("user", null, values);
                context.startActivity(new Intent(context, MainActivity.class));
                context.finish();
            }

            @Override
            public void onFailure(Call<LoginRespVO> call, Throwable throwable) {
                Toast.makeText(context, "登陆失败!", Toast.LENGTH_SHORT).show();
                v.setEnabled(true);
                context.refreshCaptchaCodeImage();
            }
        });
    }
}
